# -*- coding: utf-8 -*-
# from __future__ import unicode_literals
from __future__ import print_function
import paramiko
import os
import sys
import oss2
import datetime
import shutil
import ConfigParser
import pytoml as toml
config = ConfigParser.RawConfigParser()
ROOT = os.path.dirname(os.path.realpath(__file__))

AccessKeyId="LTAI4GKcsc8DFzWt91UD2EtR"
AccessKeySecret="DwSysw7h62Dnn8q85jXHPZG9AqVkoX"
Endpoint="oss-cn-beijing.aliyuncs.com"
bucket="yunlumao"
def percentage(consumed_bytes, total_bytes):
    if total_bytes:
        rate = int(100 * (float(consumed_bytes) / float(total_bytes)))
        print('\r{0}% '.format(rate), end='')
        sys.stdout.flush()
print('连接阿里云oss服务器')
print(AccessKeyId)
print(AccessKeySecret)
print(Endpoint)
print(bucket)
auth = oss2.Auth(AccessKeyId, AccessKeySecret)
service = oss2.Service(auth, Endpoint)
bucket = oss2.Bucket(auth, Endpoint, bucket)
def buildCreator(btype):
    build = "--path "+ppath+" --build platform="+btype+";debug=false;autoCompile=true"
    compile = "--path "+ppath+" --compile platform="+btype+";debug=false"
    b = """
        /Applications/CocosCreator/Creator/2.3.2/CocosCreator.app/Contents/MacOS/CocosCreator %s
        """ % (build)
    os.system(b)
def buildWechatgame():
    buildCreator('wechatgame')
    print ('打包wechatgame')
def copyWechatgame():
    if not os.path.exists(os.path.join(ROOT, '../remote-assets_'+version)):
        os.mkdir(os.path.join(ROOT, '../remote-assets_'+version))
    pass
    c = """
        sh ./buildcocos_wechatgame.sh %s %s
        """ % (ppath,version)
    os.system(c)
    print ('粘贴文件')
def deleteWechatgame():
    if not os.path.exists(os.path.join(ROOT, '../remote-assets_'+version)):
        os.mkdir(os.path.join(ROOT, '../remote-assets_'+version))
    pass
    d = """
        sh ./delete.sh %s
        """ % (ppath)
    os.system(d)
    print ('删除文件')
version = raw_input("请输入版本号:")
ppath = raw_input("请输入项目路径:")
ppath = ppath.rstrip()
buildWechatgame()
print ("打包完成,修改代码")
codePath = ppath + '/build/wechatgame/src/project.js'
resPath =  ppath + '/build/wechatgame/res/import'
finalPath1 = codePath + ' \'\' ' + 'KanChe ' + resPath
print('代码路径:'+codePath)
print('资源路径:'+resPath)
print('change2:'+finalPath1)
s = """
    node ./changejs2.js %s
""" % (finalPath1)
os.system(s)
finalPath2 = codePath + ' KanChe 4' + ' Http,Dispatcher,ylm_baseScene,ylm_hallScene,ylm_lodingScene,ylm_loginScene,ylm_mapScene,ylm_mineScene,shenheScene ' + resPath
print('change1:'+finalPath2)
t = """
    node ./changejs1.js %s
""" % (finalPath2)
os.system(t)
finalPath3 = codePath + ' \'\' ' + '4'
print('finalPath3:'+finalPath3)
u = """
    node ./addcode.js %s
""" % (finalPath3)
os.system(u)
copyWechatgame()
version_dir = os.path.join(ROOT,'../remote-assets_'+version)
print('上传版本号:'+version)
print('上传路径:'+version_dir)
# 遍历所有的子目录进行上传
for (root, dirs, files) in os.walk(version_dir):
    for filename in files:
        fpath = os.path.join(root, filename)
        fdir, fname = os.path.split(fpath)
        if not os.path.isdir(fpath) and not fname.startswith("."):
            subpath = fpath.replace(version_dir, "")
            if subpath.startswith("/"):
                subpath = subpath.replace("/", '', 1)

            with open(fpath, 'rb') as fileobj:
                bucket.put_object(subpath, fileobj.read(),
                progress_callback=percentage)
            pass
print('\n上传完成,删除项目build下res')
deleteWechatgame()

